<!DOCTYPE html>
<html lang='zh-CN'>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>WinA</title>
    <link rel="stylesheet" type="text/css" href="css/api.css" />
    <link rel="stylesheet" type="text/css" href="css/box.css" />
    <script type="text/javascript" src="script/public.js"></script>
    <style>
    	p{
    		color:#999;
    		padding:5px;
    	}
    	body{
    		background-color:#fff;
    	}
    </style>
    </head>
    <body>
    </body>
    <script type='text/javascript'>
    
    //var globalUUID = '66:02:89:82:3E:25';
    var globalUUID = 'CF:FD:C5:28:20:26';
    var addrs = null;
    //初始化
    function initBle(){
	     var ble = api.require('ble');
		ble.initManager({
		  single:true,
		},function( ret ){
		   api.alert( {msg:JSON.stringify( ret )});
		});
    }
    //扫描
    function scan(){
	     var ble = api.require('ble');
		ble.scan({
		   //serviceUUIDs:['00001800-0000-1000-8000-00805f9b34fb']
		   clean:true,
		}, function( ret ){
		    if( ret.status ){
		        alert( '扫描成功' );
		    }else{
		    	alert( '扫描failed' );
		    }
		});
    }

    //扫描状态;
    function isScanning(){
	     var ble = api.require('ble');
		ble.isScanning(function( ret ){
		   api.alert( {msg:JSON.stringify( ret )});
		});
    }
    //停止扫描;
    function stopScan(){
	     var ble = api.require('ble');
		ble.stopScan(function( ret ){
		   api.alert( {msg:JSON.stringify( ret )});
		});
    }
    
    
    
    
        //停止扫描;
    function clean(){
	     var ble = api.require('ble');
		ble.clean();
    }
    function getPeripheral(){
    	 var ble = api.require('ble');
		ble.getPeripheral({
		},function( ret,err ){
		   		api.alert( {msg:'getPeripheral : ' + JSON.stringify(ret)});
		});
    
    
    
    }
    
    //测试连接；
    function connect(){
	    var ble = api.require('ble');
		ble.connect({
			peripheralUUID: globalUUID,		
		},function( ret,err ){
			api.alert( {msg:'connect : ' + JSON.stringify(ret)});
		});
    }
    //断开连接;
    function disconnect(){
    
	    var ble = api.require('ble');
		ble.disconnect({
			peripheralUUID:globalUUID,		
		},function( ret){
			ble.isConnected({
			 	peripheralUUID:globalUUID
			},function( ret){
		   		api.alert( {msg:'isConnect : ' + JSON.stringify( ret )});
			})
		});
		
    }
   
  	//连接状态;
    function isConnectedxxxx(){
	     var ble = api.require('ble');
		ble.isConnected({
			 peripheralUUID:globalUUID
		},function( ret){
		   api.alert( {msg:JSON.stringify( ret )});
		});
    }
    //发现连接设备的服务；
    function discoverService(){
	     var ble = api.require('ble');
		ble.discoverService({
			peripheralUUID:globalUUID		
		},function( ret,err){
		  api.alert( {msg:JSON.stringify( ret )});
		  console.log(JSON.stringify(ret));
		});
    }
    //根据指定的外围设备 UUID 及其服务 UUID 获取该外围设备的所有特征（Characteristic）
    function discoverCharacteristics(){
	     var ble = api.require('ble');
		ble.discoverCharacteristics({
			peripheralUUID:globalUUID,		
			serviceUUID:'0000ffb0-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		console.log(JSON.stringify(ret));
		 // writeValueForCharacteristic();
		 api.alert( {msg:JSON.stringify( ret )});
		   console.log(JSON.stringify(ret));
		});
    }  
    
      function writeValueForCharacteristic(){
	     var ble = api.require('ble');
		ble.writeValueForCharacteristic({
			//value:'617975303064',
			//唤醒设备
			value:'acfffe150100cce0',
			//value:'AYU00D',
			writeType:'withoutResponse',
			peripheralUUID: globalUUID,
			serviceUUID:'0000ffb0-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'0000ffb1-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		  // api.alert( {msg:JSON.stringify( ret )});
		   //writeValueForCharacteristic1();
		});
    }
    
         function writeValueForCharacteristic1(){
	     var ble = api.require('ble');
		ble.writeValueForCharacteristic({
			//value:'617975303064',
			value:'ab081400000000c7',
			//value:'AYU00D',
			writeType:'withoutResponse',
			peripheralUUID: globalUUID,
			serviceUUID:'0000ffb0-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'0000ffb1-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		   //api.alert( {msg:JSON.stringify( ret )});
		});
    }
    
    
    
       function writeValueForCharacteristic2(){
	     var ble = api.require('ble');
		ble.writeValueForCharacteristic({
			//value:'617975303064',
			value:'ab081202000000c6',
			//value:'AYU00D',
			writeType:'withoutResponse',
			peripheralUUID: globalUUID,
			serviceUUID:'0000ffb0-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'0000ffb1-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		   api.alert( {msg:JSON.stringify( ret )});
		});
    }
    
    
           function writeValueForCharacteristic3(){
	     var ble = api.require('ble');
		ble.writeValueForCharacteristic({
			//value:'617975303064',
			value:'ab081104000000c8',
			//value:'AYU00D',
			writeType:'withoutResponse',
			peripheralUUID: globalUUID,
			serviceUUID:'0000ffb0-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'0000ffb1-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		   api.alert( {msg:JSON.stringify( ret )});
		});
    }
    
      //根据指定的外围设备 UUID 及其服务 UUID 和特征 UUID 读取数据
      function readValueForCharacteristic(){
	     var ble = api.require('ble');
		ble.readValueForCharacteristic({
			peripheralUUID:globalUUID,
			serviceUUID:'00001800-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'00002a04-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		   api.alert( {msg:JSON.stringify( ret )});
		   
		});
    }
    
    
    //根据指定的外围设备 UUID 及其服务 UUID 和特征 UUID 获取该外围设备的所有描述符（Descriptor）
    function discoverDescriptorsForCharacteristic(){
	     var ble = api.require('ble');
		ble.discoverDescriptorsForCharacteristic({
			peripheralUUID:globalUUID,
			serviceUUID:'00001800-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'00002a00-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		if(ret.status){
			//readValueForCharacteristic();
		    	// var msg= ret.descriptors;
			// api.toast({
		          // msg:msg
		        //});
		}
		   //api.alert( {msg:JSON.stringify( ret )});
		   //api.alert( {msg:JSON.stringify( err )});
		});
    }
    
    
  
    
      //根据指定的外围设备 UUID 及其服务 UUID 和特征 UUID 及其描述符获取数据
    function readValueForDescriptor(){
	     var ble = api.require('ble');
		ble.readValueForDescriptor({
			peripheralUUID:globalUUID,
			serviceUUID:'00001800-0000-1000-8000-00805f9b34fb',
			characteristicUUID:'00002a04-0000-1000-8000-00805f9b34fb',
			descriptorUUID:'00002900-0000-1000-8000-00805f9b34fb'
		},function( ret,err){
		   api.alert( {msg:JSON.stringify( ret )});
		   //api.alert( {msg:JSON.stringify( err )});
		});
    }
    
     function notify() {
          var ble = api.require('ble');
			
	 }

	function stopNotify() {
		var ble = api.require('ble');
		ble.getAllSimpleNotifyData(function(ret) {
			console.log("获取返回值" + JSON.stringify(ret))
					//ble.clearAllSimpleNotifyData();
		});
	}
			
    </script>
    
   <body>
    <div id="wrap">
        <div id="main">
        	<br>
            <div class='itemtitle'>1.初始化蓝牙设备</div>
			<div class="clickbtn" tapmode="active" onclick="initBle()" >初始化</div> 
			<div class='itemtitle'>2.开启扫描设备</div>
			<div class="clickbtn" tapmode="active" onclick="scan()" >开启扫描scan</div>
			<div class='itemtitle'>3.获取扫描结果</div>
			<div class="clickbtn" tapmode="active" onclick="getPeripheral()" >获取到扫描后的设备数据</div>  
			<div class='itemtitle'>4.扫描状态</div>
			<div class="clickbtn" tapmode="active" onclick="isScanning()" >扫描状态isScanning</div>  
			<div class='itemtitle'>5.停止扫描</div>
			<div class="clickbtn" tapmode="active" onclick="stopScan()" >停止扫描stopScan</div>
			<div class='itemtitle'>clean</div>
			<div class="clickbtn" tapmode="active" onclick="clean()" >clean</div>
			
			<div class='itemtitle'>6.连接</div>
			<div class="clickbtn" tapmode="active" onclick="connect()" >连接connect</div>    
			<div class='itemtitle'>7.断开连接</div>
			<div class="clickbtn" tapmode="active" onclick="disconnect()" >断开连接disconnect</div>
			<div class='itemtitle'>8.连接状态</div>	   
			<div class="clickbtn" tapmode="active" onclick="isConnectedxxxx()" >连接状态isConnected</div>  
			<div class='itemtitle'>9.发现服务</div>
			<div class="clickbtn" tapmode="active" onclick="discoverService()" >发现服务discoverService</div>
			<div class='itemtitle'>10.发现特征服务</div>
			<div class="clickbtn" tapmode="active" onclick="discoverCharacteristics()" >发现特征discoverCharacteristics</div> 
			<div class='itemtitle'>11.根据指定的外围设备 UUID 及其服务 UUID 和特征 UUID 获取该外围设备的所有描述符（Descriptor）</div>
			<div class="clickbtn" tapmode="active" onclick="discoverDescriptorsForCharacteristic()" >discoverDescriptorsForCharacteristic</div>
			<div class='itemtitle'>12.根据指定的外围设备 UUID 及其服务 UUID 和特征 UUID 读取数据</div>
			<div class="clickbtn" tapmode="active" onclick="readValueForCharacteristic()" >readValueForCharacteristic</div>
			<div class='itemtitle'>13.readValueForDescriptor</div>
			<div class="clickbtn" tapmode="active" onclick="readValueForDescriptor()" >readValueForDescriptor</div> 
			<div class='itemtitle'>14.writeValueForCharacteristic</div>
			<div class="clickbtn" tapmode="active" onclick="writeValueForCharacteristic()" >writeValueForCharacteristic</div>
			<div class='itemtitle'>14.writeValueForCharacteristic1</div>
			<div class="clickbtn" tapmode="active" onclick="writeValueForCharacteristic1()" >writeValueForCharacteristic1</div>
			
			<div class='itemtitle'>14.writeValueForCharacteristic2</div>
			<div class="clickbtn" tapmode="active" onclick="writeValueForCharacteristic2()" >writeValueForCharacteristic2</div>
			
				<div class='itemtitle'>称重模式</div>
			<div class="clickbtn" tapmode="active" onclick="writeValueForCharacteristic3()" >writeValueForCharacteristic3</div>
			
			
			
			<div class='itemtitle'>14.notify</div>
			<div class="clickbtn" tapmode="active" onclick="notify()" >notify</div>
			<div class='itemtitle'>14.stopNotify</div>
			<div class="clickbtn" tapmode="active" onclick="stopNotify()" >stopNotify</div>
		</div>
	</div>
</body>

</html>